<?php

namespace app\index\controller;

use think\Db;

class Project extends \Common
{

    # 获取项目
    public function GetProjects()
    {
        $role = $this->token["role"];
        if ($role == 1) {
            if (isset($_GET["page"]) && isset($_GET["limit"]) && isset($_GET["search"])) {
                if ($_GET["search"] != "") {
                    $admin = Db::name("project")
                        ->page($_GET["page"], $_GET["limit"])
                        ->select();
                    $total = Db::name("project")
                        ->count();
                } else {
                    $admin = Db::name("project")
                        ->where("name like '%{$_GET["search"]}%'")
                        ->page($_GET["page"], $_GET["limit"])
                        ->select();
                    $total = Db::name("project")
                        ->where("name like '%{$_GET["search"]}%'")
                        ->count();
                }
                $list = [];
                foreach ($admin as $value) {
                    $list[] = [
                        "id"       => (int)$value["pid"],
                        "name"     => $this->lan == "zh" ? $value["name"] : $value["name_jp"],
                        "nameCn"   => $value["name"],
                        "nameJp"   => $value["name_jp"],
                        "url"      => $value["url"],
                    ];
                }
                $data = [
                    "list"  => $list,
                    "total" => $total
                ];
                $this->Code(200, false, $data);
            } else {
                $this->Code(403, 403);
            }
        } else {
            $this->Code(200, false, "对不起,您没有权限");
        }
    }

    # 修改用户信息
    public function UpdateProject()
    {
        $role = $this->token["role"];
        $aid = $this->token["aid"];
        $pattern = "#(http|https)://(.*\.)?.*\..*#i";
        if ($role == 1) {
            if (isset($_POST["id"])) {
                if (isset($_POST["name"]) && isset($_POST["url"])) {

                    # 验证是否为网址
                    if (!preg_match($pattern, $_POST["url"])) {
                        $this->Code(403, "请填写正确的网址");
                    }

                    $str = substr($_POST["url"], -1);
                    if ($str != "/") {
                        $_POST["url"] .= "/";
                    }
                    $upInfo = [
                        "name"  => $_POST["nameCn"],
                        "name_jp"  => $_POST["nameJp"],
                        "url"   => $_POST["url"]
                    ];
                    $check = Db::name("project")
                        ->where("pid != {$_POST["id"]}")
                        ->where(function ($query) {
                            $query->where("name", $_POST["name"])->whereOr("url", $_POST["url"]);
                        })
                        ->find();
                    if (empty($check)) {
                        Db::name("project")
                            ->where("pid", $_POST["id"])
                            ->update($upInfo);
                        $this->Code(200, "修改成功");
                    } else {
                        $this->Code(403, "昵称或者地址重复,请核对后修改");
                    }
                } else {
                    $this->Code(403, 403);
                }
            } else {
                if (isset($_POST["name"]) && isset($_POST["url"])) {

                    # 验证是否为网址
                    if (!preg_match($pattern, $_POST["url"])) {
                        $this->Code(403, "请填写正确的网址");
                    }

                    $str = substr($_POST["url"], -1);
                    if ($str != "/") {
                        $_POST["url"] .= "/";
                    }
                    $upInfo = [
                        "name"  => $_POST["nameCn"],
                        "name_jp"  => $_POST["nameJp"],
                        "url"   => $_POST["url"]
                    ];
                    $check = Db::name("project")
                        ->where(function ($query) {
                            $query->where("name", $_POST["name"])->whereOr("url", $_POST["url"]);
                        })
                        ->find();
                    if (empty($check)) {
                        Db::name("project")
                            ->insert($upInfo);
                        $this->Code(200, "添加成功");
                    } else {
                        $this->Code(403, "昵称或者地址重复,请核对后添加");
                    }
                } else {
                    $this->Code(403, 403);
                }
            }
        } else {
            $this->Code(200, false, "对不起,您没有权限");
        }
    }

    # 删除用户
    public function DeleteProject()
    {
        $role = $this->token["role"];
        if ($role == 1) {
            if (isset($_POST["id"])) {
                Db::name("project")
                    ->where("pid", $_POST["id"])
                    ->delete();
                Db::name("distribution")
                    ->where("pid", $_POST["id"])
                    ->delete();
                $this->Code(200, "删除成功");
            } else {
                $this->Code(403, 403);
            }
        } else {
            $this->Code(200, false, "对不起,您没有权限");
        }
    }

    # 获取后台Tree
    public function GetAuthTree()
    {
        $project = Db::name("project")
            ->select();
        $data = [];
        foreach ($project as $value) {
            $auth = [
                [
                    "id" => $value["pid"] . "r",
                    "name" => "r",
                    "value" => false
                ],
                [
                    "id" => $value["pid"] . "w",
                    "name" => "w",
                    "value" => false
                ]
            ];
            if ($value["otherName"] != "") {
                $otherArr = explode(",", $value["otherName"]);
                foreach ($otherArr as $item) {
                    $auth[] = [
                        "id" => $value["pid"] . $item,
                        "name" => $item,
                        "value" => false
                    ];
                }
            }
            $data[] = [
                "id" => $value["pid"],
                "name" => $value["name"],
                "auth" => $auth
            ];
        }
        $this->Code(200, false, $data);
    }
}
